#include "gtx31x_api.h"
#include "gtx31x_app.h"
#include "gtx31x_iic.h"

#if _KEYBOARD_FUNCTION_ENABLE_
/**
 * @brief 键值转换
 *
 * @return char - 转换后键值
 */
static char Gtx31xKeyValueConversion(void);

/**
 * @brief 按键触发中断回调
 *
 */
static void Gtx31xKeyTiggerIrqCallback(void);

/**
 * @brief 获取键入键值回调
 *
 * @return Gtx31xKeyScanfResult_t - 键入结果
 */
static Gtx31xKeyScanfResult_t Gtx31xGetKeyValueCallback(void);

/**
 * @brief 获取本地注册键入键值回调
 *
 * @return Gtx31xKeyScanfResult_t - 键入结果
 */
static Gtx31xKeyScanfResult_t Gtx31xGetLocalRegisterKeyValueCallback(void);

/**
 * @brief 键入管理员密码键值回调
 *
 * @return Gtx31xKeyScanfResult_t - 键入结果
 */
static Gtx31xKeyScanfResult_t Gtx31xGetAdministratorPasswordKeyValueCallback(void);

/**
 * @brief 获取退出本地注册标志回调
 *
 * @return Gtx31xKeyScanfResult_t - 键入结果
 */
static Gtx31xKeyScanfResult_t Gtx31xGetLocalRegisterExitKeyValueCallback(void);

/**
 * @brief 设备自检任务回调
 *
 */
static void Gtx31xSelfTestCallback(void);

/**
 * @brief 数据交互任务回调
 *
 */
static void Gtx31xDataInteractionCallback(void);

/**
 * @brief 输入禁用超时回调
 *
 */
static void Gtx31xKeyscanfTimeoutCallback(void);

/**
 * @brief 键值转换表
 *
 */
#if _BOARD_TYPE_ == _V31_A_BOARD_
const uint16_t KeyValueConversionTable[GTX31X_CHAEENL_BIT_ALL] = {
    [GTX31X_CHANNEL_BIT_1] = 0x0400,  // 0
    [GTX31X_CHANNEL_BIT_2] = 0x0004,  // 1
    [GTX31X_CHANNEL_BIT_3] = 0x0008,  // 2
    [GTX31X_CHANNEL_BIT_4] = 0x0040,  // 3
    [GTX31X_CHANNEL_BIT_5] = 0x0002,  // 4
    [GTX31X_CHANNEL_BIT_6] = 0x0010,  // 5
    [GTX31X_CHANNEL_BIT_7] = 0x0080,  // 6
    [GTX31X_CHANNEL_BIT_8] = 0x0001,  // 7
    [GTX31X_CHANNEL_BIT_9] = 0x0020,  // 8
    [GTX31X_CHANNEL_BIT_10] = 0x0100, // 9
    [GTX31X_CHANNEL_BIT_11] = 0x0800, // *
    [GTX31X_CHANNEL_BIT_12] = 0x0200, // #
};

#elif _BOARD_TYPE_ == _V33_A_BOARD_
const uint16_t KeyValueConversionTable[GTX31X_CHAEENL_BIT_ALL] = {
    [GTX31X_CHANNEL_BIT_1] = 0x0001,  // 0
    [GTX31X_CHANNEL_BIT_2] = 0x0010,  // 1
    [GTX31X_CHANNEL_BIT_3] = 0x0020,  // 2
    [GTX31X_CHANNEL_BIT_4] = 0x0200,  // 3
    [GTX31X_CHANNEL_BIT_5] = 0x0008,  // 4
    [GTX31X_CHANNEL_BIT_6] = 0x0080,  // 5
    [GTX31X_CHANNEL_BIT_7] = 0x0100,  // 6
    [GTX31X_CHANNEL_BIT_8] = 0x0004,  // 7
    [GTX31X_CHANNEL_BIT_9] = 0x0040,  // 8
    [GTX31X_CHANNEL_BIT_10] = 0x0400, // 9
    [GTX31X_CHANNEL_BIT_11] = 0x0002, // *
    [GTX31X_CHANNEL_BIT_12] = 0x0800, // #
};

#elif _BOARD_TYPE_ == _V33_B_BOARD_
const uint16_t KeyValueConversionTable[GTX31X_CHAEENL_BIT_ALL] = {
    [GTX31X_CHANNEL_BIT_1] = 0x0100,  // 0
    [GTX31X_CHANNEL_BIT_2] = 0x0002,  // 1
    [GTX31X_CHANNEL_BIT_3] = 0x0004,  // 2
    [GTX31X_CHANNEL_BIT_4] = 0x0010,  // 3
    [GTX31X_CHANNEL_BIT_5] = 0x0001,  // 4
    [GTX31X_CHANNEL_BIT_6] = 0x0008,  // 5
    [GTX31X_CHANNEL_BIT_7] = 0x0020,  // 6
    [GTX31X_CHANNEL_BIT_8] = 0x0800,  // 7
    [GTX31X_CHANNEL_BIT_9] = 0x0200,  // 8
    [GTX31X_CHANNEL_BIT_10] = 0x0040, // 9
    [GTX31X_CHANNEL_BIT_11] = 0x0400, // *
    [GTX31X_CHANNEL_BIT_12] = 0x0080, // #
};

#elif _BOARD_TYPE_ == _V33_C_BOARD_
const uint16_t KeyValueConversionTable[GTX31X_CHAEENL_BIT_ALL] = {
    [GTX31X_CHANNEL_BIT_1] = 0x0800,  // 0
    [GTX31X_CHANNEL_BIT_2] = 0x0008,  // 1
    [GTX31X_CHANNEL_BIT_3] = 0x0010,  // 2
    [GTX31X_CHANNEL_BIT_4] = 0x0020,  // 3
    [GTX31X_CHANNEL_BIT_5] = 0x0004,  // 4
    [GTX31X_CHANNEL_BIT_6] = 0x0040,  // 5
    [GTX31X_CHANNEL_BIT_7] = 0x0080,  // 6
    [GTX31X_CHANNEL_BIT_8] = 0x0002,  // 7
    [GTX31X_CHANNEL_BIT_9] = 0x0200,  // 8
    [GTX31X_CHANNEL_BIT_10] = 0x0100, // 9
    [GTX31X_CHANNEL_BIT_11] = 0x0001, // *
    [GTX31X_CHANNEL_BIT_12] = 0x0400, // #
};

#endif

static Gtx31xStatusBit_t Gtx31xStatusBit = {0};
const Gtx31xKeyScanfFunction_t KeyScanfFunction[] = {
    [GTX31X_NORMAL] = Gtx31xGetKeyValueCallback,
    [GTX31X_LOCAL_REGISTER] = Gtx31xGetLocalRegisterKeyValueCallback,
    [GTX31X_LOCAL_REGISTER_EXIT] = Gtx31xGetLocalRegisterExitKeyValueCallback,
    [GTX31X_KEYIN_ADMIN_PASSWORD] = Gtx31xGetAdministratorPasswordKeyValueCallback,
};

static char Gtx31xKeyValueConversion(void)
{
  uint16_t SamplingValue = Gtx31xGetTouchDetectionBit();
  PRINT("[gtx31x]: get key value: %04X.\r\n", SamplingValue);

  if ((SamplingValue != 0x0000) && (SamplingValue != 0xFFFF))
  {
    for (uint8_t KeyNumber = 0; KeyNumber < GTX31X_CHAEENL_BIT_ALL; KeyNumber++)
    {
      if (SamplingValue == KeyValueConversionTable[KeyNumber])
      {
        switch (KeyNumber)
        {
        case GTX31X_CHANNEL_BIT_11:
          return '*';

        case GTX31X_CHANNEL_BIT_12:
          return '#';

        default:
          return KeyNumber + '0';
        }
      }
    }
  }

  return 0;
}

static void Gtx31xKeyTiggerIrqCallback(void)
{
  if (Gtx31xGetDeviceBusyEn() != true)
  {
    if (Gtx31xGetExternalVerificationSensorEn() == true)
    {
      if (Gtx31xStatusBit.WakeupEn == false)
      {
        if (Gtx31xReportWakeup() == true)
          return;

        PRINT("[gtx31x]: report wakeup.\r\n");
        Gtx31xStatusBit.WakeupEn = true;
      }
      else
      {
        if (Gtx31xStatusBit.StandbyEn == false)
        {
          if (Gtx31xReportIntoStandby() != true)
            return;

          PRINT("[gtx31x]: report into standby.\r\n");
          Gtx31xStatusBit.StandbyEn = true;
        }
        else
        {
          Gtx31xStatusBit.OperationStep = GTX31X_GET_KEY_VALUE;
          Gtx31xDelayedStartDataInteractionTask(GTX31X_GET_KEY_VALUE_WAIT_TIME);
        }
      }
    }
    else
    {
      if (Gtx31xStatusBit.StandbyEn == false)
      {
        if (Gtx31xReportIntoStandby() != true)
          return;

        PRINT("[gtx31x]: report into standby.\r\n");
        Gtx31xStatusBit.StandbyEn = true;
      }
      else
      {
        Gtx31xStatusBit.OperationStep = GTX31X_GET_KEY_VALUE;
        Gtx31xDelayedStartDataInteractionTask(GTX31X_GET_KEY_VALUE_WAIT_TIME);
      }
    }
  }
  else
  {
    Gtx31xStatusBit.OperationStep = GTX31X_GET_KEY_VALUE;
    Gtx31xDelayedStartDataInteractionTask(GTX31X_GET_KEY_VALUE_WAIT_TIME);
  }
}

static Gtx31xKeyScanfResult_t Gtx31xGetKeyValueCallback(void)
{
  Gtx31xKeyScanfResult_t KeyScanfResult = GTX31X_INVALID_KEY_ENTRY;

  if (Gtx31xStatusBit.KeyValue != 0)
  {
    Gtx31xPlayKeypadTone();

    switch (Gtx31xStatusBit.KeyValue)
    {
    case '#':
    {
      KeyScanfResult = GTX31X_KEY_ENTERED;
    }
    break;

    case '*':
    {
      KeyScanfResult = GTX31X_EXIT_CURRENT_MODE;

      if (Gtx31xStatusBit.UserTypedKeys.NumberOfKey > 0)
      {
        KeyScanfResult = GTX31X_WAIT_FOR_USER_ENTER_KEY;

        Gtx31xStatusBit.UserTypedKeys.NumberOfKey = 0;
        memset(Gtx31xStatusBit.UserTypedKeys.KeyValue, 0, GTX31X_NUMBER_OF_MAX_KEYSCANF);
      }
    }
    break;

    default:
    {
      KeyScanfResult = GTX31X_KEY_ENTERED;

      if (Gtx31xStatusBit.UserTypedKeys.NumberOfKey < GTX31X_NUMBER_OF_MAX_KEYSCANF - 1)
      {
        KeyScanfResult = GTX31X_WAIT_FOR_USER_ENTER_KEY;
        Gtx31xStatusBit.UserTypedKeys.KeyValue[Gtx31xStatusBit.UserTypedKeys.NumberOfKey++] = Gtx31xStatusBit.KeyValue;
      }
    }
    break;
    }
  }

  return KeyScanfResult;
}

static Gtx31xKeyScanfResult_t Gtx31xGetLocalRegisterKeyValueCallback(void)
{
  Gtx31xKeyScanfResult_t LocalRegisterResult = GTX31X_INVALID_KEY_ENTRY;

  if (Gtx31xStatusBit.KeyValue != 0)
  {
    Gtx31xPlayKeypadTone();
    Gtx31xReloadLocalRegisterTimer();

    switch (Gtx31xStatusBit.KeyValue)
    {
    case '#':
    {
      if (Gtx31xStatusBit.UserTypedKeys.KeyedInformation.WaitForVerification == true)
      {
        LocalRegisterResult = GTX31X_KEY_ENTERED;

        if (memcmp(Gtx31xStatusBit.UserTypedKeys.KeyValue, Gtx31xStatusBit.UserTypedKeys.KeyedInformation.KeyedValue, Gtx31xStatusBit.UserTypedKeys.NumberOfKey) != 0)
        {
          Gtx31xPlayKeyComparisonErrorSound();
          LocalRegisterResult = GTX31X_WAIT_FOR_PLAYBACK_FINISH;
        }
      }
      else
      {
        LocalRegisterResult = GTX31X_WAIT_FOR_PLAYBACK_FINISH;

        if (Gtx31xStatusBit.UserTypedKeys.NumberOfKey == NUMBER_OF_KEYBOARD_KEY)
        {
          Gtx31xStatusBit.UserTypedKeys.KeyedInformation.WaitForVerification = true;
          memcpy(Gtx31xStatusBit.UserTypedKeys.KeyedInformation.KeyedValue, Gtx31xStatusBit.UserTypedKeys.KeyValue, NUMBER_OF_KEYBOARD_KEY);

          Gtx31xUpdateLocalEntryStatus(GTX31X_REGISTER_CONFIRM);
        }
        else
        {
          Gtx31xUpdateLocalEntryStatus(GTX31X_REGISTER_START);
        }
      }

      if (LocalRegisterResult != GTX31X_KEY_ENTERED)
      {
        Gtx31xStatusBit.UserTypedKeys.NumberOfKey = 0;
        memset(Gtx31xStatusBit.UserTypedKeys.KeyValue, 0, GTX31X_NUMBER_OF_MAX_KEYSCANF);
      }
      else
      {
        Gtx31xUpdateLocalEntryStatus(GTX31X_REGISTER_END);
      }
    }
    break;

    case '*':
    {
      if (Gtx31xStatusBit.UserTypedKeys.NumberOfKey > 0)
      {
        LocalRegisterResult = GTX31X_WAIT_FOR_USER_ENTER_KEY;

        Gtx31xStatusBit.UserTypedKeys.NumberOfKey = 0;
        memset(Gtx31xStatusBit.UserTypedKeys.KeyValue, 0, GTX31X_NUMBER_OF_MAX_KEYSCANF);
      }
      else
      {
        LocalRegisterResult = GTX31X_EXIT_CURRENT_MODE;

        if (Gtx31xStatusBit.UserTypedKeys.KeyedInformation.WaitForVerification == true)
        {
          LocalRegisterResult = GTX31X_WAIT_FOR_PLAYBACK_FINISH;
          memset(&Gtx31xStatusBit.UserTypedKeys, 0, sizeof(Gtx31xUserTypedKeys_t));

          Gtx31xUpdateLocalEntryStatus(GTX31X_REGISTER_START);
        }
      }
    }
    break;

    default:
    {
      LocalRegisterResult = GTX31X_WAIT_FOR_USER_ENTER_KEY;
      Gtx31xStatusBit.UserTypedKeys.KeyValue[Gtx31xStatusBit.UserTypedKeys.NumberOfKey++] = Gtx31xStatusBit.KeyValue;

      if (Gtx31xStatusBit.UserTypedKeys.NumberOfKey > NUMBER_OF_KEYBOARD_KEY)
      {
        Gtx31xPlayTheKeyEntryErrorSoundEffect();
        LocalRegisterResult = GTX31X_WAIT_FOR_PLAYBACK_FINISH;

        Gtx31xStatusBit.UserTypedKeys.NumberOfKey = 0;
        memset(Gtx31xStatusBit.UserTypedKeys.KeyValue, 0, GTX31X_NUMBER_OF_MAX_KEYSCANF);
      }
    }
    break;
    }
  }

  return LocalRegisterResult;
}

static Gtx31xKeyScanfResult_t Gtx31xGetAdministratorPasswordKeyValueCallback(void)
{
  Gtx31xKeyScanfResult_t VerifyResult = GTX31X_INVALID_KEY_ENTRY;

  if (Gtx31xStatusBit.KeyValue != 0)
  {
    Gtx31xPlayKeypadTone();

    switch (Gtx31xStatusBit.KeyValue)
    {
    case '#':
    {
      VerifyResult = GTX31X_KEY_ENTERED;
    }
    break;

    case '*':
    {
      VerifyResult = GTX31X_EXIT_CURRENT_MODE;

      if (Gtx31xStatusBit.UserTypedKeys.NumberOfKey > 0)
      {
        VerifyResult = GTX31X_WAIT_FOR_USER_ENTER_KEY;

        Gtx31xStatusBit.UserTypedKeys.NumberOfKey = 0;
        memset(Gtx31xStatusBit.UserTypedKeys.KeyValue, 0, GTX31X_NUMBER_OF_MAX_KEYSCANF);
      }
    }
    break;

    default:
    {
      VerifyResult = GTX31X_KEY_ENTERED;

      if (Gtx31xStatusBit.UserTypedKeys.NumberOfKey < (GTX31X_NUMBER_OF_MAX_KEYSCANF - 1))
      {
        VerifyResult = GTX31X_WAIT_FOR_USER_ENTER_KEY;
        Gtx31xStatusBit.UserTypedKeys.KeyValue[Gtx31xStatusBit.UserTypedKeys.NumberOfKey++] = Gtx31xStatusBit.KeyValue;
      }
    }
    break;
    }
  }

  return VerifyResult;
}

static Gtx31xKeyScanfResult_t Gtx31xGetLocalRegisterExitKeyValueCallback(void)
{
  Gtx31xKeyScanfResult_t LocalRegisterResult = GTX31X_INVALID_KEY_ENTRY;

  if (Gtx31xStatusBit.KeyValue == '*')
  {
    Gtx31xPlayKeypadTone();
    LocalRegisterResult = GTX31X_EXIT_CURRENT_MODE;
  }

  return LocalRegisterResult;
}

static void Gtx31xSelfTestCallback(void)
{
  bool SelftestResult = (Gtx31xGetChipId() == GTX31X_IIC_ADDRESS) ? true : false;
  Gtx31xReportgSeltTestResult(SelftestResult);
}

static void Gtx31xDataInteractionCallback(void)
{
  switch (Gtx31xStatusBit.OperationStep)
  {
  case GTX31X_GET_KEY_VALUE:
  {
    Gtx31xStatusBit.KeyValue = Gtx31xKeyValueConversion();

    if (KeyScanfFunction[Gtx31xStatusBit.KeyscanfMode] != NULL)
    {
      Gtx31xKeyScanfResult_t KeyScanfResult = KeyScanfFunction[Gtx31xStatusBit.KeyscanfMode]();

      switch (KeyScanfResult)
      {
      case GTX31X_KEY_ENTERED:
      {
        Gtx31xDiableKeyboardFunction(GTX31X_KEYBOARD_DISABLE_TIMEOUT);

        Gtx31xStatusBit.UserTypedKeys.KeyValue[Gtx31xStatusBit.UserTypedKeys.NumberOfKey++] = '#';
        Gtx31xReportUserTypedKeys(Gtx31xStatusBit.UserTypedKeys.KeyValue, Gtx31xStatusBit.UserTypedKeys.NumberOfKey);
        PRINT("[gtx31x]: key number :%d, value: %s.\r\n", Gtx31xStatusBit.UserTypedKeys.NumberOfKey, Gtx31xStatusBit.UserTypedKeys.KeyValue);
      }
      break;

      case GTX31X_EXIT_CURRENT_MODE:
      {
        Gtx31xReportExitTyping();
        Gtx31xDiableKeyboardFunction(GTX31X_KEYBOARD_DISABLE_TIMEOUT);
      }
      break;

      case GTX31X_WAIT_FOR_PLAYBACK_FINISH:
      {
        PRINT("[gtx31x]: wait for the voice to finish playing.\r\n");

        Gtx31xDiableKeyboardFunction(0);
        Gtx31xStartDataInteractionTaskNoWait();
        Gtx31xStatusBit.OperationStep = GTX31X_GET_VOICE_PLAYBACK_STATUS;
      }
      break;

      default:
        Gtx31xResetKeyTypedTimeout();
        break;
      }
    }
  }
  break;

  case GTX31X_GET_VOICE_PLAYBACK_STATUS:
  {
    if (Gtx31xGetPlaybackCompletionFlag() == false)
    {
      Gtx31xStatusBit.OperationStep = GTX31X_GET_KEY_VALUE;
      Gtx31xEnableKeyboardFunction();
    }
    else
    {
      Gtx31xDelayedStartDataInteractionTask(GTX31X_QUERY_STATUS_BIT_INTERVAL);
    }
  }
  break;

  default:
    break;
  }
}

static void Gtx31xKeyscanfTimeoutCallback(void)
{
  Gtx31xEnableKeyboardFunction();
}
#endif

void Gtx31xLoadDefaultConfiguration(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  Gtx31xSetSleepMode(GTX31X_SLEEP_ENABLE);
  Gtx31xSetSleepMode(GTX31X_SLEEP_DISABLE);

  Gtx31xSetInterruptOperationMode(GTX31X_LEVEL_MODE);
  Gtx31xSetTouchEngineMode(GTX31X_SINGLE_TOUCH_MODE);
  Gtx31xSetSensorIdleTime(GTX31X_IDLE_H_500_MSEC, GTX31X_IDLE_L_10_MSEC);
  Gtx31xSlideModeEnableBit(GTX31X_SLIDE_ENABLE);
  Gtx31xWriteSpecialRegister(0x66, 0x02);
  Gtx31xWriteSpecialRegister(0x40, 0x02);
  Gtx31xWriteSpecialRegister(0x41, 0x01);
  Gtx31xWriteSpecialRegister(0x42, 0x01);
  Gtx31xWriteSpecialRegister(0x43, 0x00);
  Gtx31xWriteSpecialRegister(0x44, 0x02);
  Gtx31xWriteSpecialRegister(0x45, 0x03);
  Gtx31xWriteSpecialRegister(0x46, 0x00);
  Gtx31xWriteSpecialRegister(0x47, 0x02);
  Gtx31xSetCalibrationTime(GTX31X_CAL_500_MSEC);

  uint8_t KeyboardSensitivity = Gtx31xGetKeyboardSensitivity();
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_1, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_2, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_3, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_4, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_5, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_6, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_7, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_8, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_9, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_10, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_11, KeyboardSensitivity);
  Gt312SetTouchSensitivityControlBit(GTX31X_CHANNEL_BIT_12, KeyboardSensitivity);
#endif
}

void Gtx31xIntoSleepMode(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  memset(&Gtx31xStatusBit, 0, sizeof(Gtx31xStatusBit_t));
  Gtx31xSetSensorIdleTime(GTX31X_IDLE_H_500_MSEC, GTX31X_IDLE_L_10_MSEC);
#endif
}

void Gtx31xIntoStandbyMode(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  Gtx31xSetSensorIdleTime(GTX31X_IDLE_H_100_MSEC, GTX31X_IDLE_L_10_MSEC);
  Gtx31xDiableKeyboardFunction(GTX31X_KEYBOARD_WAKEUP_DELAY_TIME);
#endif
}

bool Gtx31xStartDeviceSelfTestTask(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  Gtx31xStartDeviceSelfTestTaskNoWait();
  return true;

#else
  return false;
#endif
}

bool Gtx31xStartLocalRegisterTask(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  memset(&Gtx31xStatusBit, 0, sizeof(Gtx31xStatusBit_t));

  Gtx31xStatusBit.StandbyEn = true;
  Gtx31xStatusBit.KeyscanfMode = GTX31X_LOCAL_REGISTER;
  Gtx31xStatusBit.OperationStep = GTX31X_GET_VOICE_PLAYBACK_STATUS;

  Gtx31xIntoStandbyMode();
  Gtx31xStartDataInteractionTaskNoWait();
  Gtx31xPreparationsBeforeLocalRegister();

  return true;
#else
  return false;
#endif
}

void Gtx31xLocalRegisterExitListerConfig(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  memset(&Gtx31xStatusBit, 0, sizeof(Gtx31xStatusBit_t));

  Gtx31xStatusBit.StandbyEn = true;
  Gtx31xStatusBit.KeyscanfMode = GTX31X_LOCAL_REGISTER_EXIT;
  Gtx31xStatusBit.OperationStep = GTX31X_GET_VOICE_PLAYBACK_STATUS;

  Gtx31xIntoStandbyMode();
  Gtx31xStartDataInteractionTaskNoWait();
#endif
}

void Gtx31xVerifyUserPermissionTask(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  memset(&Gtx31xStatusBit, 0, sizeof(Gtx31xStatusBit_t));

  Gtx31xStatusBit.StandbyEn = true;
  Gtx31xStatusBit.KeyscanfMode = GTX31X_NORMAL;
  Gtx31xStatusBit.OperationStep = GTX31X_GET_VOICE_PLAYBACK_STATUS;

  Gtx31xIntoStandbyMode();
  Gtx31xStartDataInteractionTaskNoWait();
#endif
}

void Gtx31xStartKeyInAdminPassword(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  memset(&Gtx31xStatusBit, 0, sizeof(Gtx31xStatusBit_t));

  Gtx31xStatusBit.StandbyEn = true;
  Gtx31xStatusBit.KeyscanfMode = GTX31X_KEYIN_ADMIN_PASSWORD;
  Gtx31xStatusBit.OperationStep = GTX31X_GET_VOICE_PLAYBACK_STATUS;

  Gtx31xIntoStandbyMode();
  Gtx31xStartDataInteractionTaskNoWait();
#endif
}

void Gtx31xEnableKeyboardFunction(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  uint8_t RegisterValue = 0, SetErrorCount = GTX31X_GET_REGISTER_FAILURE_TIMES;

  while (SetErrorCount--)
  {
    Gtx31xSetSleepMode(GTX31X_SLEEP_DISABLE);
    Gtx31xDelayMs(1);

    RegisterValue = Gtx31xGetSleepRegister();
    if ((RegisterValue & GTX31X_SLEEP_DISABLE) == GTX31X_SLEEP_DISABLE)
      break;
  }

  Gtx31xEnableTouchInterrupts();
#endif
}

void Gtx31xDiableKeyboardFunction(uint32_t DisableTime)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  uint8_t RegisterValue = 0, SetErrorCount = GTX31X_GET_REGISTER_FAILURE_TIMES;

  while (SetErrorCount--)
  {
    Gtx31xSetSleepMode(GTX31X_SLEEP_ENABLE);
    Gtx31xDelayMs(1);

    RegisterValue = Gtx31xGetSleepRegister();
    if ((RegisterValue & GTX31X_SLEEP_ENABLE) == GTX31X_SLEEP_ENABLE)
      break;
  }

  Gtx31xMaskedTouchInterrupts();

  if (DisableTime > 0)
  {
    Gtx31xStartKeyscanfDiabledTimer(DisableTime);
  }
#endif
}

void Gtx31xConfiguration(void)
{
#if _KEYBOARD_FUNCTION_ENABLE_
  Gtx31xExternalInterfaces_t ExternalInterfaces = {0};
  ExternalInterfaces.IrqCallback = Gtx31xKeyTiggerIrqCallback;
  ExternalInterfaces.DeviceSelfTestTask = Gtx31xSelfTestCallback;
  ExternalInterfaces.DataInteractionTask = Gtx31xDataInteractionCallback;
  ExternalInterfaces.KeyboardDisableTimeout = Gtx31xKeyscanfTimeoutCallback;
  Gtx31xBoardInitialisation(&ExternalInterfaces);

  Gtx31xLoadDefaultConfiguration();
  memset(&Gtx31xStatusBit, 0, sizeof(Gtx31xStatusBit_t));
#endif
}
